"""
@FileName：money_flow_daily_spider1.py\n
@Description：\n
@Author：马瑞
@Time：2024/4/14 16:55\n
@Copyright：©2019-2024 研发中心
"""
import scrapy
from lxml import etree

from 爬虫.eastmoney.eastmoney.items import money_flow_daily

class hs300_stocks_spider(scrapy.Spider):  # 继承scrapy.Spider类
    name = 'money_flow_daily_mr'  # spider名称
    allowed_domains = ['data.eastmoney.com']  # 允许此爬虫抓取的域的字符串的可选列表，指定一个列表可以抓取，其它就不会抓取了
    start_urls = ['https://data.eastmoney.com/zjlx/688981.html']  # 爬取起始页，当没有指定特定网址时，爬虫将开始抓取的网址列表
    # https://data.eastmoney.com/zjlx/688981.html
    # 指定pipelines，如果不指定，则在settings中申明了多少，就用多少
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }

    def parse(self, response):
        item = money_flow_daily()  # 定义Item类的对象，用于保存一条数据
        html = etree.HTML(response.text)
        # print(html)
        # print("哦哦哦哦哦哦")
        #处理数据
        def value_empty(value):
            if (value):
                #替换万和%为空
                return value[0].replace("万","").replace("%","")
            else:
                return 0

        item['net_amount'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[2]/span/text()'))  # 净额
        item['net_percentage'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[3]/span/text()'))  # 净占比//*[@id="table_ls"]/table/tbody/tr[1]/td[3]/span
        item['main_net_inflow'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[4]/span/text()'))  # 今日主力净流入//*[@id="table_ls"]/table/tbody/tr[1]/td[4]/span
        item['large_net_inflow'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[6]/span/text()'))  # 今日超大单净流入
        item['big_net_inflow'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[8]/span/text()'))  # 今日大单净流入
        item['medium_net_inflow'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[10]/span/text()'))  # 今日中单净流入
        item['small_net_inflow'] = value_empty(html.xpath(
            '//*[@id="table_ls"]/table/tbody/tr[1]/td[12]/span/text()'))  # 今日小单净流入
        yield item

        # 获取表格数据
        # rows = html.xpath('//*[@id="table_ls"]/table/tbody/tr')  #  -->多行

        # for row in rows:
        #     # print("哈哈哈哈哈")
        #     cols = row.xpath('.//td')
        #     item['net_amount'] = cols[1].xpath('.//span/text()')  # 净额
        #     item['net_percentage'] = cols[2].xpath('.//span/text()')  # 净占比
        #     item['main_net_inflow'] = cols[3].xpath('.//span/text()')  # 今日主力净流入
        #     item['large_net_inflow'] = cols[5].xpath('.//span/text()')  # 今日超大单净流入
        #     item['big_net_inflow'] = cols[7].xpath('.//span/text()')  # 今日大单净流入
        #     item['medium_net_inflow'] = cols[9].xpath('.//span/text()')  # 今日中单净流入
        #     item['small_net_inflow'] = cols[11].xpath('.//span/text()')  # 今日小单净流入
        #     # print(cols[1].xpath('.//a/text()'))
        #     yield item